﻿

namespace Mulala.Cavan.Application.MainBoundedContext.ProprietorModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.AttachmentModule;
    using Spring.Context;
    using Spring.Context.Support;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;

    public class ProprietorAppService : BaseAppService<Proprietor, ProprietorSearch, IProprietorRepository>, IProprietorAppService
    {
        private IProprietorStatusRepository ProprietorStatusRepository { get; set; }
        private IAttachmentAppService AttachmentAppService { get; set; }
        private IApplicationContext SpringContext = null;

        #region IProprietorAppService Members

        public override BoolGenericResult<Proprietor> Update(Proprietor changedProprietor)
        {
            BoolGenericResult<Proprietor> result = new BoolGenericResult<Proprietor>();

            Proprietor currentProprietor = this.Repository.Get(changedProprietor.ID);
            result = currentProprietor.ModifyInfo(changedProprietor.Name, changedProprietor.NamePrivacy, changedProprietor.ContactEmail, changedProprietor.ContactEmailPrivacy,
                changedProprietor.Area, changedProprietor.Telephone, changedProprietor.TelephonePrivacy, changedProprietor.Address, changedProprietor.AddressPrivacy, changedProprietor.Introduce);

            this.Repository.Update(currentProprietor);

            return result;
        }

        public BoolGenericResult<Attachment> UpdateProprietorAvatar(Proprietor changedProprietor, string fileUrl, string fileName)
        {
            this.SpringContext = ContextRegistry.GetContext();
            this.AttachmentAppService = this.SpringContext.GetObject("AttachmentAppService") as IAttachmentAppService;

            BoolGenericResult<Attachment> result = new BoolGenericResult<Attachment>();
            Attachment avatar = changedProprietor.Attachments.FirstOrDefault(a => a.AttachmentGroup == AttachmentIdentifier.AttachmentGroup.ProprietorAvatar);

            if (null == avatar)
            {
                avatar = AttachmentFactory.CreateAttachment(AttachmentIdentifier.AttachmentGroup.ProprietorAvatar, changedProprietor.ID, fileName, fileUrl, true, AttachmentIdentifier.CheckedStatus.UnChecked,
                    false, 1, DateTime.Now, changedProprietor.UserAccount.Name);
                this.AttachmentAppService.Add(avatar);

                result.Object = avatar;
            }
            else
            {
                result = changedProprietor.ModifyAvatar(fileUrl, fileName);
                if (result.IsSuccess)
                {
                    this.AttachmentAppService.Update(result.Object);
                }
            }
            result.Messages.Add("保存成功！");
            return result;
        }
        #endregion



    }
}
